package com.egu.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.egu.mapper.ClassSubjectMapper;
import com.egu.model.ClassSubject;
import com.egu.model.ClassSubjectExample;
import com.egu.service.ClassSubjectService;
import com.egu.vo.PageParam;
import com.egu.vo.RequestPageVo;
import com.egu.vo.RequestResultVo;

@Service
public class ClassSubjectServiceImpl implements ClassSubjectService {
	@Autowired
	ClassSubjectMapper classSubjectMapper;

	@Override
	public RequestPageVo<ClassSubject> getList(PageParam param, ClassSubject classSubject) {
		RequestPageVo<ClassSubject> result = new RequestPageVo<ClassSubject>();
		ClassSubjectExample example = new ClassSubjectExample();
		com.egu.model.ClassSubjectExample.Criteria criteria = example.createCriteria();
		criteria.andIsUsedEqualTo("1");
		if (StringUtils.isNotBlank(classSubject.getSubjectName())) {
			criteria.andSubjectNameLike("%" + classSubject.getSubjectName() + "%");
		}

		int count = (int) classSubjectMapper.countByExample(example);
		if (count > 0) {
			example.setStartPage(param.getOffset());
			example.setPageSize(param.getLimit());
			List<ClassSubject> list = classSubjectMapper.selectByExample(example);
			result.setRows(list);
			result.setTotal(count);
		}
		return result;
	}

	@Override
	@Transactional
	public RequestResultVo<ClassSubject> saveOrUpdate(ClassSubject classSubject) {
		if (classSubject.getId() == null) {
			classSubjectMapper.insertSelective(classSubject);
		} else {
			classSubjectMapper.updateByPrimaryKeySelective(classSubject);
		}
		return RequestResultVo.success();
	}

	@Override
	@Transactional
	public RequestResultVo<ClassSubject> deleteDataById(Integer id) {
		ClassSubject classSubject = new ClassSubject();
		classSubject.setId(id);
		classSubject.setIsUsed("0");
		try {
			classSubjectMapper.updateByPrimaryKeySelective(classSubject);
		} catch (Exception e) {
			return RequestResultVo.error("数据库删除失败");
		}
		return RequestResultVo.success();
	}

	@Override
	public ClassSubject getDataById(Integer id) {
		ClassSubject subject = classSubjectMapper.selectByPrimaryKey(id);
		return subject;
	}

	@Override
	public RequestResultVo<List<ClassSubject>> getAllList() {
		ClassSubjectExample example = new ClassSubjectExample();
		example.createCriteria().andIsUsedEqualTo("1");
		List<ClassSubject> list = classSubjectMapper.selectByExample(example);
		return RequestResultVo.success(list);
	}
	
	
	
}
